package com.cloud.wms.ui.in.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.text.TextUtils;
import android.widget.TextView;

import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.in.contract.InContract;
import com.cloud.wms.ui.in.entity.TaskDetailEntity;
import com.cloud.wms.ui.in.presnter.InPresenter;
import com.cloud.wms.ui.main.entity.TaskEntity;

import butterknife.BindView;

/**
 * @Author:Chen
 * @Date: 2021-01-19 16:45
 * @Description:
 */
public class InOrderDetailActivity extends BaseBarListActivity<TaskDetailEntity.DetailsBean, InPresenter> implements InContract.View {

    @BindView(R.id.tv_in_code)
    TextView tvInCode;
    @BindView(R.id.tv_in_type)
    TextView tvInType;
    @BindView(R.id.tv_in_sku)
    TextView tvInSku;
    @BindView(R.id.tv_in_amount)
    TextView tvInAmount;
    @BindView(R.id.tv_in_create)
    TextView tvInCreate;
    private TaskEntity taskEntity;

    @Override
    protected String barTitle() {
        return "查看详情";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_in_detail;
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_in_detail;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        taskEntity = getIntent().getParcelableExtra(IntentKey.OBJECT_KEY);

        mPresenter.loadInDetail(taskEntity);
    }

    @Override
    public void onReload() {
        super.onReload();
        mPresenter.loadInDetail(taskEntity);
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, TaskDetailEntity.DetailsBean item) {
        helper.setText(R.id.tv_commodity_name, item.getCommodityName())
                .setText(R.id.tv_commodity_batch, item.getProductBatchCode())
                .setText(R.id.tv_commodity_amount, item.getPlanQty() + item.getUnitName())
                .setText(R.id.tv_commodity_type, item.getStockTypeName())
                .setBackgroundResource(R.id.tv_commodity_type, item.getStockTypek() == 1 ? R.drawable.shape_in_detail_good : R.drawable.shape_in_detail_bad)
                .setTextColorRes(R.id.tv_commodity_name, item.getStockTypek() == 1 ? R.color.font_green : R.color.font_red)
                .setGone(R.id.tv_batch_title, TextUtils.isEmpty(item.getProductBatchCode()))
                .setGone(R.id.tv_commodity_batch, TextUtils.isEmpty(item.getProductBatchCode()));
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onSuccess(Message message) {
        super.onSuccess(message);
        if (message.what == InPresenter.IN_DETAIL) {
            TaskDetailEntity inDetail = (TaskDetailEntity) message.obj;
            mAdapter.setNewInstance(inDetail.getDetails());

            tvInAmount.setText(inDetail.getCommodityQty());
            tvInCode.setText(inDetail.getInCode());
            tvInCreate.setText(inDetail.getCreatedAt());
            tvInType.setText(inDetail.getInType());
            tvInSku.setText(inDetail.getSkuQty());
        }
    }

    public static void start(Context context, TaskEntity entity) {
        Intent intent = new Intent(context, InOrderDetailActivity.class);
        intent.putExtra(IntentKey.OBJECT_KEY, entity);
        context.startActivity(intent);
    }
}
